/**
 * Created by 小润 on 2017/3/27.
 */
$(document).ready(function(){
//  var ajaxData,shareList,feedId,userId;
//  init();
//
////初始化页面
//function init(){
//  $.ajax({
//      url: '/api/v1/person/details',
//      type: "post",
//      async: true,
//      success: function (data) {
//          ajaxData = data;
//          initPage(data);
//          console.log(data.result);
//      },
//      error: function (msg) {
//          //alert('请求失败');
//          console.log(msg)
//      }
//  });
//}
//点赞or取消赞
$('.dianzan').click(isLike);

//创建评论
$('.comment').click(commentEdit);

//查看帖子详情
/*$('#share .container').click(shareDetail);

//帖子详情
function shareDetail(shareList){
    var index = $(this).index();
    feedId = shareList[index].id;
    var data = {
        "feedId": feedId,
        "userId": userId
    };
    $.ajax({
        type: "get",
        url: '/api/v1/feedService/get/one',
        data:data,
        success: function (data) {

        },
        error: function (msg) {
            alert('请求失败');
            console.log(msg)
        }
    });

}*/

//点赞or取消赞
function isLike(){
        var likeCount = $(this).children('.dianzan-count').text();
        var liked=$(this).children('.liked').val();
        var fid=$(this).children(".fid").val();
        // alert(" liked:"+liked);
        // alert("likecount:"+likeCount);
        // alert("fid:"+fid);
        //var content = $('#editContent').val();
        var obj=$(this)
        var data = {
            /* "customerId": 1,*/
            "id": fid,
            /*"feedId":2,
             "userId":3,*/
           /* "likeCount":likeCount*/
        };
        if (liked==0){
            $.ajax({
                type: "get",
                url: '/api/v1/feedService/like/create',
                data:data,
                success: function (data) {
                    obj.children('img').attr('src','/static/h5/images/dianzan.png');
                    alert("点赞成功！");
                    //点赞成功之后把值变成1
                    obj.children(".liked").val(1);
                    obj.children(".dianzan-count").text(" ");
                    likeCount=parseInt(likeCount)+parseInt(1);                   
                    obj.children(".dianzan-count").text(likeCount);
                    //likeCount=likeCount+1;
                    //alert("点数量增加1");

                },
                error: function (msg) {
                    alert('请求失败');
                    console.log(msg)
                }
            });
        }
        else{
            $.ajax({
                type: "post",
                url: '/api/v1/feedService/like/delete',
                data:data,
                success: function (data) {
                    obj.children('img').attr('src','/static/h5/images/dianzan1.png');
                    alert("取消成功！");
                    obj.children(".liked").val(0);
                    obj.children(".dianzan-count").text(" ");
                    likeCount=likeCount-1;
                    obj.children(".dianzan-count").text(likeCount);
                },
                error: function (msg) {
                    alert('请求失败');
                    console.log(msg)
                }
            });
        }
    }



//新增评论
function commentEdit() {

      var index = $(this).parents("li").index()//点击第几条数据的检索值

	  var obj=$(this)
	  var disCount = $(this).children('.pinglun-count').text();
	  
      $(".cmt_cont").val("")//初始化

      $(".cmt_btm").animate({
         bottom: "0"
      }, 500)

      // 发表评论
      $("#cmt_issue").on("click",function () {

         var toArea = $("#share").children("ul").children("li").eq(index).find(".cmt_area")//添加数据位置
         console.log(toArea)

         var feedId = $("#contId").val()//消息id
         var content = $(".cmt_cont").val()//内容
         var data ={
            "content":content,
            /*"feedId":2*/
            "id":2
//          "disCount":disCount
         }
         
         if(content.trim().length==0 || content==null){

            alert("请输入内容")
            return false;
         }else{
         	
            $.ajax({
               type:"get",
               url:"/api/v1/feedService/comment/create",
               data:data,
               success:function(data){
                  alert("issue success")
                  
              	  obj.children(".pinglun-count").text(" ");
                  disCount=parseInt(disCount)+parseInt(1);
                  obj.children(".pinglun-count").text(disCount);
                  
                  toArea.prepend('<p class="issue_cont"><span class="issue_id">小于</span><span>：</span><span>'+data.data.content+'</span></p >')

					
                  $(".cmt_area").animate({
                     bottom: "-40px"
                  },500)
               },
               error:function(err){
                  alert("issue failed")
               }

            })
         }
      })

      return false
   }
	//阻止事件冒泡
	    $(".cmt_btm").on("click",function(){
	    	return false
	    })
	    
	    $("body").on("click",function(){
			$(".cmt_btm").animate({
	    		bottom:"-40px"
	    	},500)
		})

});

//历程
$(function(){
	$("#up_Img").on('click',function(){
		$(".popup").animate({
			bottom:"0"
		},1000)
	})
	
	$(".cancle").on("click",function(){
		$(".popup").animate({
			bottom:"-220px"
		},1000)
	
	})
	//发布图片
	$(".issue_img").on("click",function(){
		$(".popup").hide()
		$(".process").hide()
		$(".frame_box").show()
		$("#iss").show()
		$(".popup").animate({
			bottom:"-220px"
		},1000)
		
		$(".frame_box textarea").val("")//清除之前样式
		$("#Img").attr("src","../../images/photo-load.png")
	})
//	发布历程
	$("#iss").on("click",function(){
		
		if($("#Img").attr("src") == "../../images/photo-load.png"){
			alert("图片不能为空")
		}else{
			
			var $year,$day
			var $fName = $(".frame_box textarea").val()
//			$.ajax({									
//				 	type: "post",
//                  url: '/api/v1/family/create',
//                  data: {
//                      fName:$fName,
//                      fDesp:$fDesp
//                  },
//                  success: function (data) {
//                   
//                      console.log(data.result);
//                      
                        $(".popup").show()
						$(".process").show()
						$(".frame_box").hide()
						$("#iss").hide()
//                  	
//                  	for (var i=0;i<data.length;i++) {
                    		
                    		
							$("#img_box").prepend('<ul class="pro_content"><li class="date"><p id="year">2017</p><p id="day">0526</p></li>'
							+'<li class="circle"></li><li class="up_box"><h5 id="explain">'+$fName+'</h5><img  id="rec_Img"/></li></ul>')
							
							$("#rec_Img").attr("src",Img.src)
//                  	}
                    	
//                  },
//                  error: function (msg) {
//                      alert('请求失败');
//                      console.log(msg)
//
//                  }
//			})
			
		}
		
	})
	
})

//发送分享
	$(".share_btn").click(createShare);
//创建分享
	function createShare(){
		$("#message_share").hide()
		$("#publish").show()
		
		$(".cancle_share").on("click",function(){
			$("#message_share").show()
			$("#publish").hide()
		})
		
		$(".issue_share button").on("click",function(){
			if($(".share_cont").val()==""){
				$(".issue_share button").attr({
					"disabled":false
				})
			}else{
				$("#share ul li").remove()
				$("#message_share").show()
				$("#publish").hide()
				
				var issMonth,issDay
				var issFont = $(".share_cont").val()
				var data = {
					"issFont":issFont,
					"issMonth":issMonth,
					"issDay":issDay,
				}
				 $.ajax({
	                type: "get",
	                url: '/api/v1/feedService/get/one',//接口地址？
	                data: data,
	                success: function (data) {
                   		console.log('请求成功');
                   		 $("#share ul").prepend('<li class="container"><div class="row"><div class="col-xs-6 share-time">'
                    +'<div><span class="share-time-day">27</span><span class="share-time-mouth">3</span>月</div></div>'
                    +'<div class="col-xs-6 share-content"><img src="../../images/share-img.png" class="share-img" alt=""/>'
                    +'<span class="share-text">'+issFont+'</span></div></div><div class="row share-method"><div class="col-xs-6 dianzan">'
                    +' <img src="../../images/dianzan1.png" alt=""/><span class="dianzan-count">16</span></div><div class="col-xs-6 comment">'
                    +'<img src="../../images/pinglun.png" alt=""/><span class="pinglun-count">6</span></div></div></li>')
	                },
	                error: function (msg) {
	                    console.log('请求失败');
	                    console.log(msg)
//	                    $("#share ul").prepend('<li class="container"><div class="row"><div class="col-xs-6 share-time">'
//                  +'<div><span class="share-time-day">27</span><span class="share-time-mouth">3</span>月</div></div>'
//                  +'<div class="col-xs-6 share-content"><img src="../../images/share-img.png" class="share-img" alt=""/>'
//                  +'<span class="share-text">'+data.issFont+'</span></div></div><div class="row share-method"><div class="col-xs-6 dianzan">'
//                  +' <img src="../../images/dianzan1.png" alt=""/><span class="dianzan-count">16</span></div><div class="col-xs-6 comment">'
//                  +'<img src="../../images/pinglun.png" alt=""/><span class="pinglun-count">6</span></div></div></li>')
	                }
            	});
				
			}
		})
	}
//分享时获取地理位置
$("#locMess").on("click",function(){
	var pos = new BMap.Geolocation()
	pos.getCurrentPosition(function(suc){
		$("#locMess").html("")
		$("#locMess").html(suc.address.street)
	})
})